const model = require('../../public/model/env.js');
const user_icon = tt.getStorageSync('user_icon') || '';
const url = model.api_pro_url();

Page({
  data: {
    background: '',
    logotext: '',
    logobrz: '',
    btn: '',
    url: '',
    lhltext1: '',
    chongsha: '',
    baiji: '',
    logintext: '',
    lhltext2: '',
    lhltext3: '',
    user_icon: user_icon,
    backgroundtext: '',
    backgroundzuozhe: '',
    myurl: '',
    showModal: false,
    showModal1: true,
    pages: '',
    rens:'',
  },
  go_conmm: function () {
    tt.navigateTo({
      url: '../conmment/conmment',
    });
  },
  onLoad: function (fromcode) {

    // console.log("onLoad", fromcode);
    tt.setStorageSync('fromcode', fromcode.fromcode);
    const truncateString = (str) => {
      if (str.length > 18) {
        return str.substring(0, 15) + '.';
      }
      return str;
    };

    const key = model.key();
    const appid = model.appid();
    const huangli = `${url}/index.php/api/relhl`;
    const requestUrl = `${url}/index.php/api/onload`;

    const options = {
      url: requestUrl,
      data: {
        appid: appid,
        key: key
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    
    const tthuanglioptions = {
      url: huangli,
      data: {
        appid: appid,
        key: key
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };

    tt.request({
      ...tthuanglioptions,
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({
            lhltext1: res.data.data[0].yinli,
            chongsha: res.data.data[0].chongsha,
            baiji: res.data.data[0].baiji,
            yi: truncateString(res.data.data[0].yi),
            ji: truncateString(res.data.data[0].ji),
          });
        }
      },
      fail: (err) => {
        console.error("请求黄历信息失败", err);
      }
    });

    tt.request({
      ...options,
      success: (res) => {
        if (res.data.code === 200) {
          console.log(res.data);
          const datajson = res.data.datajson;
          this.setData({
            background: res.data.background,
            logotext: datajson.logotext,
            logobrz: datajson.logobrz,
            logintext: datajson.logintext,
            lhltext2: datajson.lhltext2,
            lhltext3: datajson.lhltext3,
            btn: datajson.btn,
            url: datajson.url,
            rens: datajson.rens,
            backgroundtext: this.getBackgroundText(res.data.background),
            backgroundzuozhe: this.getBackgroundAuthor(res.data.background),
            // myurl: datajson.url === "input" ? 'my' : '',
            myurl: datajson.url === "input" ? 'my' :
              datajson.url === "nav" ? 'my' : '',

            pages: datajson.url === "input" ? 'pages' : datajson.url === "about" ? 'pages' : '',
            // pages: datajson.url === "input" ? 'pages' : datajson.url === "aboutindex" ? 'about' : '',
            // pages: datajson.url === "input" ? 'pages' :
            //   datajson.url === "aboutindex" ? 'about' :
            //     datajson.url === "nav" ? 'pages' : '',

            // showModal: datajson.url === "input" ? true : false,
            showModal: datajson.url === "input" || datajson.url === "nav" ? true : false,

            showModal1: datajson.url === "input" || datajson.url === "nav" ? false : true,
          });
          tt.setStorageSync('background', res.data.background);
          tt.setStorageSync('true', datajson.url);
          console.log("请求成功", this.data.pages, "请求成功", this.data.url);
        } else {
          console.error("请求返回码非200", res.data);
        }
      },
      fail: (err) => {
        console.error("请求首页信息失败", err);
      }
    });
  },
  getBackgroundText: function (background) {
    switch (background) {
      case 'day':
        return '太阳初出光赫赫，千山万山如火发。';
      case 'night':
        return '静夜沉沉，浮光霭霭，冷浸溶溶月。';
      default:
        return '';
    }
  },
  getBackgroundAuthor: function (background) {
    switch (background) {
      case 'day':
        return '—— 赵匡胤《咏初日》';
      case 'night':
        return '—— 丘处机《无俗念》';
      default:
        return '';
    }
  }
});
